<template>
  <div class="kd_login_bkg">
    <div class="kd_login">
      <h4>叩丁狼商品管理系统</h4>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" type="text" autocomplete="off">
            <template #prefix>
              <el-icon><User color="#fff" /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
          >
            <template #prefix>
              <el-icon><Unlock color="#eee" /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <!-- <el-button type="primary" @click="submitForm(ruleFormRef)" -->
          <el-button type="primary" v-btnAntiShake="()=>submitForm(ruleFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { LoginApi } from './api'
import { useRouter } from 'vue-router'
const navto = useRouter()
const ruleFormRef = ref<FormInstance>();

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    if (ruleForm.username !== "") {
      if (!ruleFormRef.value) return;
      // ruleFormRef.value.validateField("checkPass", () => null);
    }
    callback();
  }
};
// console.log(navto.getRoutes());

const ruleForm = reactive({
  username: "admin",
  password: "123456",
});
const rules = reactive({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ validator: validatePass, trigger: "blur" }],
});

const submitForm = (formEl: FormInstance | undefined) => {
  console.log('进来了');
  
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      // 请求
      LoginApi(ruleForm).then(res=>{
        console.log(res);
        
        if(res.code === 200){
          // 1、存token
          localStorage.setItem("token",res.data.tokenHead+res.data.token)
          // 2、提示
          ElMessage.success("亲，登录成功")
          // 3、跳转
          navto.push("/")
        }
      })
    } else {
      ElMessage.error("亲，请输入正确的用户密码");
      return false;
    }
    // 点击登录 清空输入框
    // ruleForm.password = ""
    // ruleForm.username = ""
  });
};
</script>

<style lang="scss" scoped>
.kd_login_bkg {
  width: 100vw;
  height: 100vh;
  background-color: #2d3a4b;
}
.kd_login {
  width: 300px;
  height: 300px;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  transform: scale(1.3);
  h4 {
    text-align: center;
    color: #fff;
    margin: 20px 0;
  }
  :deep(.el-button--primary) {
    width: 100%;
  }
  :deep(.el-input__wrapper) {
    background-color: rgb(22, 20, 44);
  }
}
</style>
